#region Using directives
using System;
using Microsoft.DirectX.Direct3D;
using System.IO;
using SharpWind.Helpers;
using Microsoft.DirectX;
#endregion

namespace SharpWind.Resources.Shaders
{
	public class Shader : Resource
	{
		#region Fields

		private Effect dxEffect = null;

		#endregion

		#region Methods

		public int Begin()
		{
			return this.dxEffect.Begin( FX.None );
		}

		public void End()
		{
			this.dxEffect.End();
		}

		public void BeginPass( int pass )
		{
			this.dxEffect.BeginPass( pass );
		}

		public void EndPass()
		{
			this.dxEffect.EndPass();
		}

		public void SetValue( string variable, SharpWind.Resources.Textures.Texture value )
		{
			this.dxEffect.SetValue( variable, value.DxTexture );
		}

		public void SetValue( string variable, Texture value )
		{
			this.dxEffect.SetValue( variable, value );
		}

		public void SetValue( string variable, Matrix value )
		{
			this.dxEffect.SetValue( variable, value );
		}

		public void SetValue( string variable, Vector4 value )
		{
			this.dxEffect.SetValue( variable, value );
		}

		public void SetValue( string variable, float value )
		{
			this.dxEffect.SetValue( variable, value );
		}

		public void Commit()
		{
			this.dxEffect.CommitChanges();
		}

		internal override bool Load( string name )
		{
			if( string.IsNullOrEmpty( name ) )
			{
				throw new ArgumentNullException( name );
			}

			string errors;
			this.dxEffect = Effect.FromFile( Engine.Instance.Graphics.Device,
											 Path.Combine( Directories.Shaders, name ),
											 null, null, ShaderFlags.None, null, out errors );

			return true;
		}

		#endregion

		#region Properties

		public string Technique
		{
			get
			{
				return this.dxEffect.Technique.ToString();
			}
			set
			{
				this.dxEffect.Technique = value;
			}
		}

		#endregion
	}
}
